<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"
export default {
  name: 'BigDataLineChart',
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    headTitle: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
    // autoResize: {
    //   type: Boolean,
    //   default: true
    // },
    // chartData: {
    //   type: Array,
    //   required: true
    // },
    // yMax: {
    //   type: Number,
    //   required: true
    // },
    // interval: {
    //   type: Number,
    //   required: true
    // }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    window.addEventListener('resize', this.resizeHandler)
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeHandler)
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        xAxis: {
          data: [380, 420, 460, 500, 540, 580, 620, 660, 700, 740, 780],
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          min: 0,
          max: 1,
          interval: 0.25,
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['expected', 'actual']
        },
        series: [{
          symbol: 'none',
          name: 'expected',
          itemStyle: {
            normal: {
              color: '#2468D5',
              lineStyle: {
                color: '#2468D5',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: [0, 0.6, 0.4, 0.7, 0.8, 1, 0.75, 0.5, 0.25, 0.2, 0],
          label: {
            position: 'top'
          },
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          symbol: 'none',
          name: 'actual',
          smooth: false,
          type: 'line',
          itemStyle: {
            normal: {
              color: new echarts.graphic.LinearGradient(
                0, 0, 1, 0,
                [
                  { offset: 0, color: '#A004AE' },
                  { offset: 0.1, color: '#1418F5' },
                  { offset: 0.3, color: '#3A9A40' },
                  { offset: 0.5, color: '#FEFE00' },
                  { offset: 0.7, color: '#F47F0e' },
                  { offset: 0.9, color: '#FF0000' },
                  { offset: 1, color: '#DE1665' }
                ]
              ),
              areaStyle: {
                // color: '#f3f8ff'
                color: {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 1,
                  y2: 0,
                  colorStops: [{
                    offset: 0, color: '#A004AE' // 100% 处的颜色
                  }, {
                    offset: 0.1, color: '#1418F5' // 90% 处的颜色
                  }, {
                    offset: 0.3, color: '#3A9A40' // 70% 处的颜色
                  }, {
                    offset: 0.5, color: '#FEFE00' // 50% 处的颜色
                  }, {
                    offset: 0.7, color: '#F47F0e' // 30% 处的颜色
                  }, {
                    offset: 0.9, color: '#FF0000' // 10% 处的颜色
                  }, {
                    offset: 1, color: '#DE1665' //   0% 处的颜色
                  }],
                  global: false // 缺省为 false
                }
              }
            }
          },
          data: [0.6, 0.5, 0.3, 0.7, 0.6, 0.9, 0.75, 0.5, 0.7, 0.5, 0.8],
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>

<style scoped>
</style>
